//
//  ViewController.m
//  导航栏渐变
//
//  Created by holier_zyq on 16/8/23.
//  Copyright © 2016年 holier_zyq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)UIImageView *heagderImageV;

@end

@implementation ViewController


- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, 375, 667+64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.tableHeaderView = self.heagderImageV;
//        _tableView.bounces = NO;
         self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
        [self.tableView addSubview:self.heagderImageV];
        
    }
    return _tableView;
}

- (UIImageView *)heagderImageV{
    if (!_heagderImageV) {
        self.heagderImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, 375, 200)];
        _heagderImageV.image = [UIImage imageNamed:@"background_2"];
        _heagderImageV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _heagderImageV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
//    self.navigationController.delegate = self;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
    self.title= @"name";

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
//
//    //放一张透明图片
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"clear"] forBarMetrics:UIBarMetricsCompactPrompt];
////    //去除黑线
//    self.navigationController.navigationBar.clipsToBounds = YES;
//
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 64)];
    barView.tag = 120;
    barView.backgroundColor = [UIColor blueColor];
    [self.navigationController.view insertSubview:barView belowSubview:self.navigationController.navigationBar];
    
    
}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    // 判断要显示的控制器是否是自己
//    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
//    
//    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"测试%ld行",indexPath.row];
//    static CGFloat initialDelay = 0.2f;
//    static CGFloat stutter = 0.06f;
    
    
    
//        cell.contentView.transform =  CGAffineTransformMakeTranslation(self.view.bounds.size.width, 0);
//        [UIView animateWithDuration:1. delay:initialDelay + ((indexPath.row) * stutter) usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
//            cell.contentView.transform = CGAffineTransformIdentity;
//        } completion:NULL];
    

    return cell;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIView *barView = [self.navigationController.view viewWithTag:120];
    
    CGFloat offsetY = scrollView.contentOffset.y+200;
    
    if (offsetY <= -30) {
        barView.alpha = 0;
    }else{
        CGFloat alpha = offsetY/200;
        if (alpha >1) {
            barView.alpha = 1;
        }else if (alpha <= 1) {
            barView.alpha = offsetY/200;
        }
        
    }
    
//    NSLog(@"%f",offsetY);
    
    CGPoint point = scrollView.contentOffset;
    if (point.y + 64 < - 200) {
        CGRect rect = self.heagderImageV.frame;
        rect.origin.y = point.y;
        rect.size.height = -point.y;
        self.heagderImageV.frame = rect;
    }
    
   
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
